package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.BalanceHistoryBO;
import com.junzhiit.nengji.cms.service.BalanceHistoryService;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.BalanceHistoryVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.BalanceHistoryDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;

@Service
public class BalanceHistoryServiceImpl extends BaseServiceImpl implements BalanceHistoryService {

	@Autowired
	private BalanceHistoryDAO balanceHistoryDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, BalanceHistoryVO balanceHistoryVO) {
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(balanceHistoryVO.getSessionId()), SystemUserSessionVO.class);
		
		BalanceHistoryBO balanceHistoryBO = new BalanceHistoryBO();
		BeanUtils.copyProperties(balanceHistoryVO, balanceHistoryBO);
		balanceHistoryBO.setCsId(systemUserSessionVO.getSystemId());
		List<BalanceHistoryBO> list = balanceHistoryDAO.findAll(dtp.getStart(), dtp.getLength(), balanceHistoryBO);
		int totalCount = balanceHistoryDAO.getTotalCount(balanceHistoryBO);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findHeaderData(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		BalanceHistoryBO balanceHistoryBO = balanceHistoryDAO.findHeaderData(systemUserSessionVO.getSystemId());
		if(balanceHistoryBO.getTotalMoney()==null){
			balanceHistoryBO.setTotalMoney(0.00);
		}
		if(balanceHistoryBO.getMoney()==null){
			balanceHistoryBO.setMoney(0.00);
		}
		result.setData(balanceHistoryBO);
		return result;
	}

}
